﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Data.SQLite;
using System.Text;

public partial class movie_index : BaseShowPage
{
    private int pageSize = 10;
    protected string MovieListHtml = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            InitMovieList();
        }
    }
    private void InitMovieList()
    {    
        DataRow row = PageData.SelectParamRow("typeCode='movie'");
         
        Master.SetTitleKeyDesc(row);

        string siteId = row[PageColumn.newsSiteId].ToString();
        string[] ids = siteId.Split(',');
        //case imgLocal when length(imgLocal) > 1 当imgLocal为null时也可以
        string sqlStart = "select id, title, case imgLocal when length(imgLocal) > 1 then imgLocal else img end as img, actor, year, area from MoviePublish ";// director,
        string sqlEnd = " order by id desc limit 0," + pageSize.ToString();
        string sql = "";
        StringBuilder sbHtml = new StringBuilder(8192);
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(conn);
        SQLiteDataReader reader = null;

        try
        {
            conn.Open();
            foreach (string sid in ids)
            {
                sql = sqlStart + " where type=" + sid.Trim() + " " + sqlEnd;
                cmd.CommandText = sql;
                bool isFirst = true;
                try
                {
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        if (isFirst)
                        {
                            isFirst = false;
                            DataRow rowSite = PageData.SelectParamRow("typeId=" + sid);
                            string siteName = rowSite[PageColumn.typeName].ToString();
                            sbHtml.Append("<div class=\"movieSite\" id=\"movie").Append(sid).Append("\"><h3>");
                            sbHtml.Append(siteName).Append("</h3><ul class=\"movieUL\">");
                        }
                        
                        sbHtml.Append("<li><div class=\"movieImgAlign\">");
                        sbHtml.Append("<a href=\"moviedetail.aspx?id=").Append(reader["id"].ToString()).Append("\" ");
                        sbHtml.Append(" title=\"").Append(reader["title"].ToString()).Append("\" target=\"_blank\">");
                        sbHtml.Append("<img src=\"").Append(reader["img"].ToString()).Append("\" alt=\"").Append(reader["title"].ToString());
                        sbHtml.Append("\" onload=\"resizeMovieImg(this, 130, 160);\" /> ").Append("</a></div>");
                        sbHtml.Append("<dl><dt><a target=\"_blank\" href=\"content.aspx?id=").Append(reader["id"].ToString()).Append("\">");
                        sbHtml.Append(reader["title"].ToString()).Append("</a></dt>");
                        sbHtml.Append("<dd>演员:").Append(reader["actor"].ToString()).Append("</dd>");
                        //sbHtml.Append("<dd>导演：").Append(reader["director"].ToString()).Append("</dd>");
                        sbHtml.Append("<dd>上映:").Append(reader["year"].ToString()).Append(" 地区:").Append(PageData.GetArea(reader["area"])).Append("</dd>");
                       
                        sbHtml.Append("</dl></li>");
                    }
                    reader.Close();
                    if (!isFirst)//isFirst = false;说明已经读取到数据，加上结束标签
                    {
                        sbHtml.Append("</ul></div>");
                    }
                    //string html = string.Format(@"
                    //<ul class=""movieUL"">                
                    //    <li> 
                    //        <div class=""movieImgAlign"">                 
                    //            <a href=""moviedetail.aspx?id={0}"" title=""{1}"" target=""_blank"">
                    //                <img src=""{2}"" alt=""{3}"" onload=""resizeMovieImg(this, 130, 160);"" />
                    //            </a>
                    //        </div>
                    //        <dl>
                    //            <dt>
                    //                <a href=""moviedetail.aspx?id={0}"" title=""{1}"" target=""_blank"">{2}</a>
                    //            </dt>
                    //            <dd>演员：{}</dd>
                    //            <dd>导演：{}</dd>
                    //            <dd>上映年份：{}</dd>
                    //            <dd>地区：{}</dd>
                    //        </dl>                        
                    //    </li>                   
                    //</ul>", 2);
                }
                catch (Exception exRead)
                {
                    if(reader != null)
                    {
                        reader.Close();
                    }
                    LogHelper.Log.Error("读取新闻数据出错。sql=" + sql, exRead);
                }
            }
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
        MovieListHtml = sbHtml.ToString();
    }    
}
